﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Db.Model.Merchant;
using Mate.Dto.Merchant;
using Mate.Framework.BaseController.Admin;
using Mate.Merchant.Service;
using Microsoft.AspNetCore.Mvc;
namespace Mate.Admin.Controllers.Merchant
{
    public class MerchantController : BaseController
    { 
        private readonly IMerchantService merchantService;
        private readonly IDistrictService districtService;
        private readonly IMerchantCategoryService merchantCategoryService;
        private readonly IMerchantTagService merchantTagService;

        private readonly ILog log;
        private readonly ICacheManager _cache;
        public MerchantController(IMerchantService merchantService, IDistrictService districtService, 
            IMerchantCategoryService merchantCategoryService, IMerchantTagService merchantTagService, ICacheManager cache, ILog log)
        {
            this.log = log;
            this.merchantService = merchantService;
            this.districtService = districtService;
            this.merchantCategoryService = merchantCategoryService;
            this.merchantTagService = merchantTagService;
            this._cache = cache;
        }

        public IActionResult Index()
        {
            return View();
        }

        #region 获取列表
        [HttpPost]
        public IActionResult GetList(int pageIndex, int pageSize, string Key)
        {
            int recordCount = 0;

            var list = merchantService.GetMerchantList(pageIndex, pageSize, Key, ref recordCount);
            var result = new
            {
                rows = list,
                total = recordCount
            };
            return Json(result);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var result = new BaseResult();

            var model = merchantService.Get(x => x.id == id);
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }
            var b = merchantService.Update(new mp_merchant()
            {
                id = id,
                is_delete = 2
            }, x => new object[] { x.is_delete }, x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "删除成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "删除失败";
            }
            return Json(result);
        }
        #endregion

        #region 添加
        public ActionResult Create()
        {
            var model = new MerchantModel() { is_open = 1 ,is_hot=1,is_brand=0};
            model.MerchantCategorieList = merchantCategoryService.GetMerchantCategoryTree(1);
            model.MerchantTagList = merchantTagService.GetTagListKv();
            model.DistrictList = districtService.GetDistrictTree(1);
            return View(model);
        }

        [HttpPost]
        public JsonResult Create(mp_merchant model)
        {
            var result = new BaseResult();
            model.is_delete = 1;
            model.is_audit = 1;
            model.createtime = DateTime.Now;
            if (model.is_brand == 0)
            {
                model.brand_name = string.Empty;
            }
            var obj = merchantService.Insert(model);
            if (obj > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "添加成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加失败";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Edit(int id)
        {
            var model = merchantService.Queryable.Select<MerchantModel>("*").First(x => x.id == id && x.is_delete == 1);

            model.MerchantCategorieList = merchantCategoryService.GetMerchantCategoryTree(1);
            model.DistrictList = districtService.GetDistrictTree(1);
            model.MerchantTagList = merchantTagService.GetTagListKv(); 
            if (model.id == 0)
            {
                return Content("参数错误");
            }
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(mp_merchant model)
        {
            var result = new BaseResult();
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }
            if (model.is_brand == 0)
            {
                model.brand_name = string.Empty;
            }

            var b = merchantService.UpdateIgnore(model,
                x => new { x.is_delete ,x.is_audit,x.createtime},
                x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "修改成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "修改失败";
            }
            return Json(result);

        }
        #endregion
    }
}